/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.UI;

import java.util.ArrayList;
import java.util.HashMap;
import javaapplication6.ControlLayer.OrderRentControl;
import javaapplication6.DomainLayer.Customer;
import javaapplication6.DomainLayer.OrderRentProduct;
import javaapplication6.DomainLayer.RentProduct;


/**
 *
 * @author meydlo
 */
public class OrderRentUI {

    private long customerId;
    private boolean enoughGoodsInStok;
    CustomerUI customerUI=new CustomerUI();
    private RentUI rentUI;
    private OrderRentControl orderRentControl;
    private Listener listener;


    public OrderRentUI(){
        rentUI=new RentUI();
        orderRentControl=new OrderRentControl();
        listener=new Listener();
        enoughGoodsInStok=true;
    }
    public void printChoices()
    {
        System.out.println(
                "\n (1) Create Order"+
                "\n (2) confirm order"+
                "\n (3) print orders"+
                "\n (4) return goods"+
                "\n (5) main menu"
                );
    }
    public void orderOrderRentProductManager()
    {
        boolean exit=false;
        while(exit==false){
            printChoices();
            int number=listener.getLineInt();
            switch(number){
                case 1:{
                    processSale();
                    break;
                }
                case 2:{
                    confirmOrder();
                    break;
                }
                case 3:{
                    printOrdersHashMapUI();
                    break;
                }
                case 4:{
                    returnedUI();
                    break;
                }
                case 5:{
                    exit=true;
                    break;
                }
                case -1:{
                    System.out.println("Please write a number");
                    break;
                }
                default:{
                    System.out.println("Valid rank id: 1-4");
                }
            }
        }
    }

    //iniciate whole process of sale
    public void processSale()
    {
        Customer customer=customerUI.getCustomerByIDui();
        if(customer!=null){
            ArrayList<OrderRentProduct> orderRentProductList = new ArrayList<OrderRentProduct>();
            customerId=customer.getId();
            createOrderRent1(customerId, orderRentProductList);
        }
    }

       //we want to put copy of goods to basket and make order
    public void createOrderRent1(long customerId, ArrayList<OrderRentProduct> orderRentProductList)
    {

        //C choosing product
        RentProduct rentProduct=rentUI.getRentProductByIDui();

        //ask for duration
        int duration=getDurationUI();

        long productId=0;

        //if product E
        if(rentProduct!=null){

            productId=rentProduct.getId();

            //how much we need?
            int noOfType=getNoOfTypeUI();


            OrderRentProduct orderRentProduct;

            //making copy of product which we need and how much we need
            orderRentProduct=orderRentControl.setOrderRentProduct(rentProduct,noOfType,customerId,duration);

            //check if are enough goods in stok to sale
            enoughGoodsInStok=orderRentControl.enoughGoodsInStok(noOfType, rentProduct);

            if(enoughGoodsInStok){

                //print info about copy of product
                orderRentProduct.printProductInfo();

                System.out.println("Put this good/s to basket?(y/n): ");
                boolean putToBasket=listener.getLineBoolean();

                if(putToBasket==true){

                    //puttting copy of goods to basket
                    orderRentProductList.add(orderRentProduct);

                    //removing original goods from stok
                    orderRentControl.removeProductFromProductList(productId,noOfType);

                    //iniciate next part of shopping process
                    continueInShopping(orderRentProductList,customerId);
                }
                else{
                    //iniciate next part of shopping process
                    continueInShopping(orderRentProductList,customerId);
                }
            }
            else{
                System.out.println("Not enough goods in stok"+
                        "\nin stok: "+rentProduct.getNoOfOneType()+"\n"
                        );

                //starting next part of shopping process
                continueInShopping(orderRentProductList,customerId);
            }
        }
    }


    public void continueInShopping(ArrayList<OrderRentProduct> orderRentProductList, long customerId)
    {
        System.out.println("continue in shopping? (y/n):");
        boolean continueInShopping=listener.getLineBoolean();
        boolean emptyBasket=orderRentProductList.isEmpty();

        //want continue
        if(continueInShopping==true){

            //continue in shopping
            createOrderRent1(customerId, orderRentProductList);

        }
        else if(!emptyBasket){
            orderRentControl.printBasket(orderRentProductList);
            System.out.println("Make order? (y/n): ");
            boolean makeOrder=listener.getLineBoolean();

            if(!orderRentControl.isEmptyBasket(orderRentProductList)){

                //handle confirmation (if confirm=true store to HashMap else clear Basket
                orderRentControl.confirmOrder(orderRentProductList, makeOrder);
            }
            else if(makeOrder){
                System.out.println("order wasn't created!");
                System.out.println("< empty basket >");
            }
            //set back no customer!
            customerId=0;
        }
        System.out.println("Ordering sucessfully canceled.");
    }

    public void confirmOrder()
    {
        //declare orderId
        long orderId=getOrderIdUI();

        if(orderRentControl.orderExist(orderId)){
            System.out.println("Want to confirm?(y) or cancel?(n)");
            boolean isConfirmed=listener.getLineBoolean();

            //handle confirm ordering (if confirmed:( move orderHashMap to LoghashMap) else: (remove from orderHashMap)
            orderRentControl.confirmToBuy(orderId, isConfirmed);
        }
        else{
            System.out.println("Order doesn't exist");
        }
    }

    public long getOrderIdUI()
    {
        System.out.println("Write OrderId: ");
        long orderId=listener.getLineInt();

        return orderId;
    }

    public long getProductIdUI()
    {
        System.out.println("Write id of Product: ");
        long productId=listener.getLineInt();
        return productId;
    }

    public int getNoOfTypeUI()
    {
        System.out.println("Write amount of this type: ");
        int noOfType=listener.getLineInt();
        return noOfType;
    }

    public void printOrdersHashMapUI()
    {
        System.out.println("print it in detail? (y/n): ");
        boolean detail=listener.getLineBoolean();
        HashMap<Long, ArrayList<OrderRentProduct>> orderRentHashMap=orderRentControl.getOrderRentHashMapC();
        orderRentControl.printOrdersHashMap(orderRentHashMap,detail);
    }

    public int getDurationUI()
    {
        System.out.println("Please write duration in (smallest period): ");
        int duration=listener.getLineInt();
        return duration;
    }

    public void returnedUI()
    {
        System.out.println("Write id of order to return: ");
        long orderId=listener.getLineInt();
        orderRentControl.returned(orderId);
    }

}
